﻿package mortal.game.view.guild.redPack
{
    import Message.Game.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.tools.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.mvc.interfaces.*;

    public class RedPacketWin extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _scaleBg:ScaleBitmap;
        private var _redPackRobInfoSp:GSprite;
        private var _moneyIcon:GImageBitmap;
        private var _titleTxt:GTextFiled;
        private var _nameTxt:GTextFiled;
        private var _redPackRobInfoBtnTxt:GTextFiled;
        private var _redPackNum:BitmapNumberText;
        private var _redPackRobInfoList:GTextFiled;
        private var _currentPart:GSprite;
        private var _state:int;
        private var _sRedEvnvelopInfo:SRedEnvelopeInfo;
        private static var _instance:RedPacketWin;

        public function RedPacketWin(param1:ILayer = null)
        {
            if (_instance != null)
            {
                throw new Error(" RedPacketWin 单例 ");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:GTextFormat = null;
            super.createDisposedChildrenImpl();
            this.setSize(415, 510);
            this._bg = UIFactory.gImageBitmap(ImagesConst.RedPackBg, 5, 33, this);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.RedPackNameBg, 130, 70, this));
            this._moneyIcon = UIFactory.gImageBitmap(ImagesConst.RedPackMoneyIcon, 184, 257, this);
            _loc_1 = GlobalStyle.textFormatBai.left();
            _loc_1.size = 17;
            this._titleTxt = UIFactory.gTextField("", 0, 5, this.width, 40, this, _loc_1);
            this._titleTxt.mouseEnabled = false;
            _loc_1 = GlobalStyle.textFormatBai.center();
            _loc_1.size = 18;
            this._nameTxt = UIFactory.gTextField("", 112, 70, 200, 40, this, _loc_1);
            _loc_1 = GlobalStyle.textFormatLv.center();
            _loc_1.underline = true;
            this._redPackRobInfoBtnTxt = UIFactory.gTextField(Language.getString(21496), 146, 404, 136, 25, this, _loc_1, true);
            this._redPackRobInfoBtnTxt.configEventListener(MouseEvent.CLICK, this.shwRobList);
            this._redPackRobInfoSp = UICompomentPool.getUICompoment(GSprite);
            this.addChild(this._redPackRobInfoSp);
            this._redPackRobInfoSp.visible = false;
            this._scaleBg = UIFactory.bg(0, 0, 120, 60, this._redPackRobInfoSp, "ToolTipBg");
            this._redPackRobInfoList = UIFactory.gTextField("", 10, 10, 250, 120, this._redPackRobInfoSp);
            this._redPackRobInfoList.defaultTextFormat = GlobalStyle.textFormatPutong;
            this._redPackRobInfoList.autoSize = TextFieldAutoSize.LEFT;
            this._redPackRobInfoList.multiline = true;
            this._redPackRobInfoList.height = 120;
            this._redPackNum = UIFactory.bitmapNumberText(148, 109, "PetGrowGreenNum.png", 21.5, 22, 0, this, 9, BitmapNumberText.Mid);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._redPackRobInfoSp.removeEventListener(MouseEvent.ROLL_OUT, this.hideSp);
            Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.hideSp);
            if (this._currentPart)
            {
                this._currentPart.dispose(param1);
                this._currentPart = null;
            }
            this._redPackRobInfoSp.dispose(param1);
            this._redPackRobInfoSp = null;
            this._redPackRobInfoList.dispose(param1);
            this._redPackRobInfoList = null;
            this._scaleBg.dispose(param1);
            this._scaleBg = null;
            this._bg.dispose(param1);
            this._bg = null;
            this._titleTxt.dispose(param1);
            this._titleTxt = null;
            this._nameTxt.dispose(param1);
            this._nameTxt = null;
            this._moneyIcon.dispose(param1);
            this._moneyIcon = null;
            this._redPackRobInfoBtnTxt.dispose(param1);
            this._redPackRobInfoBtnTxt = null;
            this._redPackNum.dispose(param1);
            this._redPackNum = null;
            this._sRedEvnvelopInfo = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function shwRobList(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (this._redPackRobInfoSp.visible == false)
            {
                this._redPackRobInfoList.htmlText = "<textFormat leading=\'4\'>" + this.getPlayerRobInfo() + "</textFormat>";
                this._redPackRobInfoList.width = this._redPackRobInfoList.textWidth + 20;
                this._redPackRobInfoList.height = this._redPackRobInfoList.textHeight + 5;
                _loc_2 = this._redPackRobInfoList.textWidth + 20;
                _loc_3 = this._redPackRobInfoList.textHeight + 20;
                this._scaleBg.setSize(_loc_2, _loc_3);
                this._redPackRobInfoSp.visible = true;
                this._redPackRobInfoSp.configEventListener(MouseEvent.ROLL_OUT, this.hideSp);
                Global.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.hideSp);
                _loc_4 = this.mouseX + 15;
                _loc_5 = this.mouseY + 15;
                if (_loc_4 + this._redPackRobInfoSp.width + this.x > SceneRange.display.width)
                {
                    _loc_4 = SceneRange.display.width - this._redPackRobInfoSp.width - this.x;
                }
                if (_loc_5 + this._redPackRobInfoSp.height + this.y > SceneRange.display.height)
                {
                    _loc_5 = SceneRange.display.height - this._redPackRobInfoSp.height - this.y;
                }
                this._redPackRobInfoSp.x = _loc_4;
                this._redPackRobInfoSp.y = _loc_5;
                this.addChild(this._redPackRobInfoSp);
            }
            return;
        }// end function

        private function hideSp(event:MouseEvent) : void
        {
            if (this._redPackRobInfoSp.visible == true)
            {
                this._redPackRobInfoSp.visible = false;
            }
            this._redPackRobInfoSp.removeEventListener(MouseEvent.MOUSE_OUT, this.hideSp);
            Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.hideSp);
            return;
        }// end function

        private function getPlayerRobInfo() : String
        {
            var _loc_2:Date = null;
            var _loc_3:Array = null;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_6:String = null;
            var _loc_7:String = null;
            var _loc_8:int = 0;
            var _loc_1:String = "";
            if (this._sRedEvnvelopInfo)
            {
                _loc_3 = [];
                _loc_4 = this._sRedEvnvelopInfo.type == 0 || this._sRedEvnvelopInfo.type == 2 ? (Language.getString(21497)) : ("");
                for (_loc_5 in this._sRedEvnvelopInfo.robInfos)
                {
                    
                    _loc_3.push({info:this._sRedEvnvelopInfo.robInfos[_loc_5], name:_loc_5});
                }
                _loc_3.sort(this.sortByDate);
                _loc_6 = "";
                _loc_7 = Cache.instance.role.entityInfo.name;
                while (_loc_8 < _loc_3.length)
                {
                    
                    _loc_2 = (_loc_3[_loc_8].info as SRobRedInfo).dt;
                    if (_loc_3[_loc_8].name == _loc_7)
                    {
                        _loc_6 = "#FAD200";
                    }
                    else
                    {
                        _loc_6 = "#1ccc31";
                    }
                    _loc_1 = _loc_1 + Language.getStringByParam(21498, DateParser.parse(_loc_2, "hh：mm：ss"), _loc_6, _loc_3[_loc_8].name, _loc_6, (_loc_3[_loc_8].info as SRobRedInfo).money, _loc_4);
                    _loc_8++;
                }
            }
            return _loc_1 ? (_loc_1) : (Language.getString(21499));
        }// end function

        private function sortByDate(param1:Object, param2:Object) : int
        {
            if (param1.info.dt.time > param2.info.dt.time)
            {
                return 1;
            }
            return -1;
        }// end function

        private function addRedPackGivePanel(param1:SRedEnvelopeInfo) : RedPackGivePanel
        {
            var _loc_2:* = UICompomentPool.getUICompoment(RedPackGivePanel);
            _loc_2.updateType(param1);
            this.addChild(_loc_2);
            return _loc_2;
        }// end function

        private function addRedPackOpenPanel(param1:SRedEnvelopeInfo) : RedPackOpenPanel
        {
            var _loc_2:* = UICompomentPool.getUICompoment(RedPackOpenPanel);
            _loc_2.updateType(param1);
            this.addChild(_loc_2);
            return _loc_2;
        }// end function

        private function addRedPackResultPanel(param1:SRedEnvelopeInfo) : RedPackRobResultPanel
        {
            var _loc_2:* = UICompomentPool.getUICompoment(RedPackRobResultPanel);
            _loc_2.updateType(param1);
            this.addChild(_loc_2);
            return _loc_2;
        }// end function

        private function addCreateRedPackPanel() : RedPackCreatePanel
        {
            var _loc_1:* = UICompomentPool.getUICompoment(RedPackCreatePanel);
            this.addChild(_loc_1);
            return _loc_1;
        }// end function

        public function updateType(param1:SRedEnvelopeInfo = null) : void
        {
            var _loc_2:String = null;
            this._state = Cache.instance.redPackCache.redPackState;
            this._sRedEvnvelopInfo = param1;
            this._moneyIcon.filters = [];
            if (this._currentPart)
            {
                this._currentPart.dispose();
                this._currentPart = null;
            }
            if (this._state == RedPackStateType.SelfeSetRedPack)
            {
                this._currentPart = this.addCreateRedPackPanel();
                this._redPackRobInfoBtnTxt.visible = false;
                this._redPackNum.text = "";
            }
            else if (this._state == RedPackStateType.CanGive || this._state == RedPackStateType.RedPackSelfGive)
            {
                this._currentPart = this.addRedPackGivePanel(this._sRedEvnvelopInfo);
                if (this._sRedEvnvelopInfo)
                {
                    this._redPackRobInfoBtnTxt.visible = true;
                }
                else
                {
                    this._redPackRobInfoBtnTxt.visible = false;
                }
            }
            else if (this._state == RedPackStateType.RedPackOpen)
            {
                this._currentPart = this.addRedPackOpenPanel(this._sRedEvnvelopInfo);
                this._redPackRobInfoBtnTxt.visible = false;
            }
            else if (this._state == RedPackStateType.HasNotGet)
            {
                this._currentPart = this.addRedPackResultPanel(this._sRedEvnvelopInfo);
                this._redPackRobInfoBtnTxt.visible = true;
                this._moneyIcon.filters = [FilterConst.colorFilter2];
            }
            else if (this._state == RedPackStateType.HasGot)
            {
                this._currentPart = this.addRedPackResultPanel(this._sRedEvnvelopInfo);
                this._redPackRobInfoBtnTxt.visible = true;
            }
            if (this._sRedEvnvelopInfo)
            {
                _loc_2 = "";
                if (this._sRedEvnvelopInfo.type == 1 || this._sRedEvnvelopInfo.type == 0)
                {
                    _loc_2 = Language.getString(35633);
                }
                else if (this._sRedEvnvelopInfo.type == 2)
                {
                    _loc_2 = Language.getString(35634);
                }
                this._titleTxt.text = Language.getStringByParam(21500, this._sRedEvnvelopInfo.boss.name + this._sRedEvnvelopInfo.totalMoney, _loc_2);
                this._nameTxt.text = this._sRedEvnvelopInfo.boss.name;
                this._redPackNum.text = param1.redCount.toString();
            }
            else
            {
                if (this._state == RedPackStateType.SelfeSetRedPack)
                {
                    this._titleTxt.text = Language.getStringByParam(21500, Cache.instance.role.entityInfo.name, Language.getString(35633));
                }
                else
                {
                    this._titleTxt.text = Language.getStringByParam(21501, Cache.instance.role.entityInfo.name);
                }
                this._nameTxt.text = Cache.instance.role.entityInfo.name;
                this._redPackNum.text = "8";
            }
            this._titleTxt.x = (this.width - this._titleTxt.textWidth) * 0.5;
            return;
        }// end function

        public function updateResult(param1:SRedEnvelopeInfo) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            this._sRedEvnvelopInfo = param1;
            if (this._currentPart)
            {
                this._currentPart.dispose();
                this._currentPart = null;
            }
            this._currentPart = this.addRedPackResultPanel(this._sRedEvnvelopInfo);
            (this._currentPart as RedPackRobResultPanel).updateResult(param1);
            var _loc_2:* = Cache.instance.role.entityInfo.name;
            for (_loc_5 in this._sRedEvnvelopInfo.robInfos)
            {
                
                if (_loc_2 == _loc_5)
                {
                    _loc_3 = (this._sRedEvnvelopInfo.robInfos[_loc_5] as SRobRedInfo).money;
                    break;
                }
            }
            if (_loc_3)
            {
                var _loc_6:* = RedPackStateType.HasGot;
                Cache.instance.redPackCache.redPackState = RedPackStateType.HasGot;
                _loc_4 = _loc_6;
            }
            else
            {
                var _loc_6:* = RedPackStateType.HasNotGet;
                Cache.instance.redPackCache.redPackState = RedPackStateType.HasNotGet;
                _loc_4 = _loc_6;
            }
            if (_loc_4 == RedPackStateType.HasGot)
            {
                this._redPackRobInfoBtnTxt.visible = true;
            }
            else if (_loc_4 == RedPackStateType.HasNotGet)
            {
                this._redPackRobInfoBtnTxt.visible = true;
                this._moneyIcon.filters = [FilterConst.colorFilter2];
            }
            return;
        }// end function

        public static function get instance() : RedPacketWin
        {
            if (_instance == null)
            {
                _instance = new RedPacketWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
